package myjava.test.bean.factory;

import java.lang.reflect.Field;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;

import myjava.test.xml.reader.bean.BeanContextReader;
import myjava.test.xml.reader.bean.BeanMessageReader;

/**
 * 更具xml中的id获取一个bean，即bean信息读取器的readAll为false
 * @author zichaoyang
 *
 */
public class GetBean extends DealBeanMessage {
	
	protected Map<String, Object> objects;

	public GetBean(String path, boolean isRootPath) {
		super(path, isRootPath);
		objects = new HashMap<>();
	}

	@SuppressWarnings("unchecked")
	@Override
	public <T> T createBean(String id) {
		return (T) getBean(initBeanMessage(id));
	}

	@Override
	public <T> T createBean(Class<?> clazz) {
		return null;
	}

	@Override
	public BeanContextReader init(String path, boolean isRootPath) {
		return new BeanMessageReader(path, isRootPath, false);
	}

	@Override
	public Map<String, LinkedList<Map<String, ?>>> initBeanMessage(String id) {
		return this.context.getBeanMessage(id);
	}
	
	public Object getBean(Map<String, LinkedList<Map<String, ?>>> bean){
		Entry<String, LinkedList<Map<String, ?>>> entry = bean.entrySet().iterator().next();
		//类的名字，包含包名
		String beanName = entry.getKey().split("&")[0];
		//类的属性及其对应的值
		LinkedList<Map<String, ?>> attrs = entry.getValue();
		return this.getBean(beanName, attrs);
	}
	
	@SuppressWarnings("unchecked")
	public Object getBean(String beanName, LinkedList<Map<String, ?>> attrs){
		if(objects.get(beanName)!=null)
			return objects.get(beanName);
		Object object = null;
		try {
			object = Class.forName(beanName).newInstance();
			for(Map<String, ?> attr : attrs){
				Entry<String, ?> e = attr.entrySet().iterator().next();
				String attributeName = e.getKey();
				Field field = object.getClass().getDeclaredField(attributeName);
				field.setAccessible(true);
				if(e.getValue() instanceof String){
					if(field.getType().getSimpleName().equals("String")){
						field.set(object, e.getValue());
					}else if(field.getType().getSimpleName().equals("Date")){
						field.set(object, new SimpleDateFormat("yyyy年MM月dd日").parse((String) e.getValue()));
					}else if(field.getType().toString().equals("boolean")){
						field.set(object, new Boolean((String) e.getValue()));
					}else if(field.getType().toString().equals("int")){
						field.set(object, Integer.parseInt((String) e.getValue()));
					}
				}else{
					if(field.getType().getSimpleName().equals("Map")){
						field.set(object, getMap(e.getValue()));
					}else if(field.getType().getSimpleName().equals("List")){
						field.set(object, getListBean(e.getValue()));
					}else{
						field.set(object, getBean((Map<String, LinkedList<Map<String, ?>>>) e.getValue()));
					}
					
				}
			}
		} catch (InstantiationException | IllegalAccessException | ClassNotFoundException | IllegalArgumentException | ParseException | NoSuchFieldException | SecurityException e) {
			e.printStackTrace();
		}
		return object;
	}

	@SuppressWarnings("unchecked")
	protected Object getListBean(Object value) {
		List<String> list = new ArrayList<>();
		List<Map<String, String>> mapValues = (List<Map<String, String>>) value;
		for(Map<String, String> map : mapValues){
			list.add(map.entrySet().iterator().next().getValue());
		}
		return list;
	}

	@SuppressWarnings("unchecked")
	protected Object getMap(Object value) {
		List<Map<String, String>> mapValues = (List<Map<String, String>>) value;
		Map<String, String> maps = new HashMap<>();
		for(Map<String, String> map : mapValues){
			maps.putAll(map);
		}
		return maps;
	}
	
}
